﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Object;

namespace Sweepids.Models.Dao
{
    public class CreditCardDao : Dao
    {
        /// <summary>
        /// Create a new credit card in the database
        /// </summary>
        /// <param name="c">The CreditCard to add</param>
        public void Create(CreditCard c)
        {
            try
            {
                var myCommand = new OracleCommand("INSERT INTO SWEEP_CREDIT_CARD " +
                                                  "(CARD_NUM, EXP_DATE, SEC_CODE) " +
                                                  "VALUES " +
                                                  "(:CARD_NUM, :EXP_DATE, :SEC_CODE)", myConnection);
                RunCommand(myCommand, c.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("CreditCardDao::Create(CreditCard): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Get a creditcard
        /// </summary>
        /// <param name="cardNum">The CreditCard number</param>
        /// <returns>A CreditCard</returns>
        public CreditCard Read(string cardNum)
        {
            try
            {
                var result = new CreditCard { CardNum = cardNum };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_CREDIT_CARD " +
                                                  "WHERE CARD_NUM=:CARD_NUM", myConnection);
                var myReader = RunCommand(myCommand, result.OracleParameters, true);
                while (myReader.Read())
                {
                    result = new CreditCard(myReader);
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("CreditCardDao::Read(string): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Update a CreditCard
        /// </summary>
        /// <param name="c">The CreditCard to update</param>
        public void Update(CreditCard c)
        {
            try
            {
                var myCommand = new OracleCommand("UPDATE SWEEP_CREDIT_CARD SET " +
                                                  "CARD_NUM=:CARD_NUM, " +
                                                  "EXP_DATE=:EXP_DATE, " +
                                                  "SEC_CODE=:SEC_CODE " +
                                                  "WHERE CARD_NUM=:CARD_NUM", myConnection);
                RunCommand(myCommand, c.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("CreditCardDao::Update(CreditCard): " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// Delete a CreditCard
        /// </summary>
        /// <param name="c">The CreditCard to delete</param>
        public void Delete(CreditCard c)
        {
            try
            {
                var myCommand = new OracleCommand("DELETE FROM SWEEP_CREDIT_CARD " +
                                                 "WHERE CARD_NUM=:CARD_NUM", myConnection);
                RunCommand(myCommand, c.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("CreditCardDao::Delete(CreditCard): " + e.Message);
                throw;
            }
        }
    }
}